//
//  ShiwuViewController.m
//  B段项目
//
//  Created by 徐茂怀 on 15/11/5.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "ShiwuViewController.h"
#import "XMHNetWorkongMethods.h"
#import "FoodIntroduceTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "ThreePictureTableViewCell.h"
#import "ContentTableViewCell.h"
#import "SingleTon.h"
#import "ShipingPlayViewController.h"
#import "AllMakeViewController.h"
#import "MBProgressHUD.h"
@interface ShiwuViewController ()
@property(nonatomic, strong)NSDictionary *dataDic;
@property(nonatomic, strong)NSArray *RecipeListArray;
@property(nonatomic, strong)MBProgressHUD *progressView;
@end

@implementation ShiwuViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil]];
    [self getData];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.bounces = NO;
    [self.view addSubview:_tableView];
    
    _progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中,不要着急";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    _progressView.backgroundColor = [UIColor colorWithRed:0.75 green:0.77 blue:0.77 alpha:0.5];
    [self.view addSubview:_progressView];
}

-(void)getData
{
    [XMHNetWorkongMethods getDataVyString:@"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446688240625&vc=77&vn=5.4.0&loguid=0&deviceid=haodou99000505722929&uuid=a6afb132369a979ba3427eec69189063&channel=xiaomi_v540&method=Stuff.getStuffInfo&virtual=&signmethod=md5&v=2&timestamp=1446707130&nonce=0.8787085766474717&appsign=2b1eea471d8ebbc476c30f4886a7861d" BodyString:[NSDictionary dictionaryWithObjectsAndKeys:_foodId,@"id", nil] WithDataBlock:^(id data) {
        NSDictionary *dic = [data objectForKey:@"result"];
        _dataDic = [dic objectForKey:@"info"];
        _RecipeListArray = [_dataDic objectForKey:@"RecipeList"];
        [_progressView removeFromSuperview];
        [_tableView reloadData];
    } WithFailure:^(NSError *error) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"网络故障" message:@"请检查网络" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [alertController removeFromParentViewController];
            [_progressView removeFromSuperview];
        }];
        UIAlertAction *going = [UIAlertAction actionWithTitle:@"继续加载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self getData];
            [alertController removeFromParentViewController];
        }];
        [alertController addAction:going];
        [self presentViewController:alertController animated:YES completion:nil];
        [alertController addAction:ok];
    }];
 

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth], NSFontAttributeName, nil];
        CGRect rect = [[_dataDic objectForKey:@"Intro" ]boundingRectWithSize:CGSizeMake(255 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        return rect.size.height + 20 * FitHeight;
    }
    else if(indexPath.row == 1)
    {
        if (_RecipeListArray.count == 0) {
            return 50 * FitHeight ;
        }
        else
        {
        return 200 * FitHeight;
        }
    }
    else if(indexPath.row == 2)
    {
        if ( ![[_dataDic objectForKey:@"Effect"] isEqual:[NSNull null]] && ![[_dataDic objectForKey:@"Effect"]isEqualToString:@""]) {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth],NSFontAttributeName, nil];
            CGRect rect = [[_dataDic objectForKey:@"Effect"]boundingRectWithSize:CGSizeMake(355 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return rect.size.height + 60 * FitHeight;
        }
        else
        {
            return 0;
        }
    }
    else if (indexPath.row == 3)
    {

        if ( ![[_dataDic objectForKey:@"Pick"] isEqual:[NSNull null]] && ![[_dataDic objectForKey:@"Pick"] isEqualToString:@""])
        {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth],NSFontAttributeName, nil];
            CGRect rect = [[_dataDic objectForKey:@"Pick"]boundingRectWithSize:CGSizeMake(355 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return rect.size.height + 60 * FitHeight;
        }
        
        else
        {
            return 0;
        }
    }
    else if (indexPath.row == 4)
    {
        if ( ![[_dataDic objectForKey:@"Skill"] isEqual:[NSNull null]] && ![[_dataDic objectForKey:@"Skill"]isEqualToString:@""] ) {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth],NSFontAttributeName, nil];
            CGRect rect = [[_dataDic objectForKey:@"Skill"]boundingRectWithSize:CGSizeMake(355 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return rect.size.height + 60 * FitHeight;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        if ( ![[_dataDic objectForKey:@"Storage"] isEqual:[NSNull null]] && ![[_dataDic objectForKey:@"Storage"]isEqualToString:@""]) {
            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15 * FitWidth],NSFontAttributeName, nil];
            CGRect rect = [[_dataDic objectForKey:@"Storage"]boundingRectWithSize:CGSizeMake(355 * FitWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
            return rect.size.height + 60 * FitHeight;
        }
        else
        {
            return 0;
        }
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *identifier = @"cell";
        FoodIntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[FoodIntroduceTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[_dataDic objectForKey:@"Cover"]]];
        [cell setContentString:[_dataDic objectForKey:@"Intro"]];
        return cell;
    }
    else if(indexPath.row == 1)
    {
        static NSString *identifier1 = @"cell1";
        ThreePictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1];
        if (!cell) {
            cell = [[ThreePictureTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier1];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.allMakeButton addTarget:self action:@selector(pushToAllMake) forControlEvents:UIControlEventTouchUpInside];
        if (_RecipeListArray.count == 0) {
            cell.makeLabel.text = [NSString stringWithFormat:@"%@的做法",self.name];
            return cell;
        }
        else if(_RecipeListArray.count == 1)
        {
            
            cell.makeLabel.text = [NSString stringWithFormat:@"%@的做法",self.name];
            [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:[_RecipeListArray[0]objectForKey:@"Cover"]]];
            cell.leftLabel.text = [_RecipeListArray[0]objectForKey:@"Title"];
            cell.leftImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftTapAction)];
            [cell.leftImageView addGestureRecognizer:tap];
            return cell;
        }
        else if (_RecipeListArray.count == 2)
        {
            cell.makeLabel.text = [NSString stringWithFormat:@"%@的做法",self.name];
            [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:[_RecipeListArray[0]objectForKey:@"Cover"]]];
            cell.leftImageView.tag = [[_RecipeListArray[0]objectForKey:@"RecipeId"]integerValue];
            cell.leftLabel.text = [_RecipeListArray[0]objectForKey:@"Title"];
            [cell.middleImageView sd_setImageWithURL:[NSURL URLWithString:[_RecipeListArray[1]objectForKey:@"Cover"]]];
            cell.middleLabel.text = [_RecipeListArray[1]objectForKey:@"Title"];
            cell.leftImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftTapAction)];
            [cell.leftImageView addGestureRecognizer:tap];
            
            cell.middleImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(middleTapAction)];
            [cell.middleImageView addGestureRecognizer:tap1];
            return cell;
        }
        else
        {
        cell.makeLabel.text = [NSString stringWithFormat:@"%@的做法",self.name];
        [cell.leftImageView sd_setImageWithURL:[NSURL URLWithString:[_RecipeListArray[0]objectForKey:@"Cover"]]];
            
        cell.leftLabel.text = [_RecipeListArray[0]objectForKey:@"Title"];
        [cell.middleImageView sd_setImageWithURL:[NSURL URLWithString:[_RecipeListArray[1]objectForKey:@"Cover"]]];
        cell.middleLabel.text = [_RecipeListArray[1]objectForKey:@"Title"];
        [cell.rightImageView sd_setImageWithURL:[NSURL URLWithString:[_RecipeListArray[2]objectForKey:@"Cover"]]];
        cell.rightLabel.text = [_RecipeListArray[2]objectForKey:@"Title"];
            cell.leftImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftTapAction)];
            [cell.leftImageView addGestureRecognizer:tap];
            
            cell.middleImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(middleTapAction)];
            [cell.middleImageView addGestureRecognizer:tap1];
            
            cell.rightImageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(rightTapAction)];
            [cell.rightImageView addGestureRecognizer:tap2];
            
        return cell;
        }
    }
    else if (indexPath.row == 2)
    {
        static NSString *identifier2 = @"cell2";
        ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier2];
        if (!cell) {
            cell = [[ContentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier2];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (![[_dataDic objectForKey:@"Effect"] isEqual:[NSNull null]]) {
            cell.titleLabel.text = [NSString stringWithFormat:@"%@的功效与作用",self.name];
            [cell setContentString:[_dataDic objectForKey:@"Effect"]];
        }
        return cell;
    }
    else if (indexPath.row == 3)
    {
        static NSString *identifier3 = @"cell3";
        ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier3];
        if (!cell) {
            cell = [[ContentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier3];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (![[_dataDic objectForKey:@"Pick"] isEqual:[NSNull null]]) {
            cell.titleLabel.text = [NSString stringWithFormat:@"如何挑选%@",self.name];
            [cell setContentString:[_dataDic objectForKey:@"Pick"]];
        }
        return cell;
    }
    else if (indexPath.row == 4)
    {
        static NSString *identifier4 = @"cell4";
        ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier4];
        if (!cell) {
            cell = [[ContentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier4];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (![[_dataDic objectForKey:@"Skill"] isEqual:[NSNull null]]) {
            cell.titleLabel.text = [NSString stringWithFormat:@"%@的吃法",self.name];
            [cell setContentString:[_dataDic objectForKey:@"Skill"]];
        }
        return cell;
    }
    else
    {
        static NSString *identifier5 = @"cell5";
        ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier5];
        if (!cell) {
            cell = [[ContentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier5];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (![[_dataDic objectForKey:@"Storage"] isEqual:[NSNull null]]) {
            cell.titleLabel.text = [NSString stringWithFormat:@"%@的制作方法",self.name];
            [cell setContentString:[_dataDic objectForKey:@"Storage"]];
        }
     
        return cell;
    }
}

-(void)pushToAllMake
{
    AllMakeViewController *vc = [[AllMakeViewController alloc]init];
    vc.name = _name;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)leftTapAction
{
    [[SingleTon shareSingleTon]setRid:[[_RecipeListArray[0]objectForKey:@"RecipeId"] stringValue]];
    ShipingPlayViewController *vc = [[ShipingPlayViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)middleTapAction
{
    [[SingleTon shareSingleTon]setRid:[[_RecipeListArray[1]objectForKey:@"RecipeId"] stringValue]];
    ShipingPlayViewController *vc = [[ShipingPlayViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)rightTapAction
{
    [[SingleTon shareSingleTon]setRid:[[_RecipeListArray[2]objectForKey:@"RecipeId"] stringValue]];
    ShipingPlayViewController *vc = [[ShipingPlayViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
